/// 6D姿态传感器   I2C通讯

#ifndef __INF_QMI8658A_H__
#define __INF_QMI8658A_H__

#include "Com_Config.h"
#include "Com_Debug.h"

// I2C 配置 数据线
#define I2C_MASTER_SDA_IO GPIO_NUM_1
// I2C 配置 时钟线
#define I2C_MASTER_SCL_IO GPIO_NUM_2
// I2C 配置 频率 100K
#define I2C_MASTER_FREQ_HZ 100000

// 6D姿态传感器 芯片地址
#define QMI8658_SENSOR_ADDR 0x6A
// 超时时间 1000ms = 1S
#define I2C_MASTER_TIMEOUT_MS 1000
// portTICK_PERIOD_MS 1ms 多少HZ
#define Inf_portTICK_PERIOD_MS ((TickType_t)1000 / CONFIG_FREERTOS_HZ)

// QMI8658寄存器地址
enum qmi8658_reg {
  QMI8658_WHO_AM_I,
  QMI8658_REVISION_ID,
  QMI8658_CTRL1,
  QMI8658_CTRL2,
  QMI8658_CTRL3,
  QMI8658_CTRL4,
  QMI8658_CTRL5,
  QMI8658_CTRL6,
  QMI8658_CTRL7,
  QMI8658_CTRL8,
  QMI8658_CTRL9,
  QMI8658_CATL1_L,
  QMI8658_CATL1_H,
  QMI8658_CATL2_L,
  QMI8658_CATL2_H,
  QMI8658_CATL3_L,
  QMI8658_CATL3_H,
  QMI8658_CATL4_L,
  QMI8658_CATL4_H,
  QMI8658_FIFO_WTM_TH,
  QMI8658_FIFO_CTRL,
  QMI8658_FIFO_SMPL_CNT,
  QMI8658_FIFO_STATUS,
  QMI8658_FIFO_DATA,
  QMI8658_STATUSINT = 45,
  QMI8658_STATUS0,
  QMI8658_STATUS1,
  QMI8658_TIMESTAMP_LOW,
  QMI8658_TIMESTAMP_MID,
  QMI8658_TIMESTAMP_HIGH,
  QMI8658_TEMP_L,
  QMI8658_TEMP_H,
  QMI8658_AX_L,
  QMI8658_AX_H,
  QMI8658_AY_L,
  QMI8658_AY_H,
  QMI8658_AZ_L,
  QMI8658_AZ_H,
  QMI8658_GX_L,
  QMI8658_GX_H,
  QMI8658_GY_L,
  QMI8658_GY_H,
  QMI8658_GZ_L,
  QMI8658_GZ_H,
  QMI8658_COD_STATUS = 70,
  QMI8658_dQW_L = 73,
  QMI8658_dQW_H,
  QMI8658_dQX_L,
  QMI8658_dQX_H,
  QMI8658_dQY_L,
  QMI8658_dQY_H,
  QMI8658_dQZ_L,
  QMI8658_dQZ_H,
  QMI8658_dVX_L,
  QMI8658_dVX_H,
  QMI8658_dVY_L,
  QMI8658_dVY_H,
  QMI8658_dVZ_L,
  QMI8658_dVZ_H,
  QMI8658_TAP_STATUS = 89,
  QMI8658_STEP_CNT_LOW,
  QMI8658_STEP_CNT_MIDL,
  QMI8658_STEP_CNT_HIGH,
  QMI8658_RESET = 96
};

// 倾角结构体
typedef struct {
  // 加速度
  int16_t acc_x;
  int16_t acc_y;
  int16_t acc_z;
  // 角速度
  int16_t gyr_x;
  int16_t gyr_y;
  int16_t gyr_z;
  // 角度
  float AngleX;
  float AngleY;
  float AngleZ;
} t_sQMI8658;

/**
 * @brief 初始化I2C
 *
 * @return esp_err_t
 */
esp_err_t bsp_i2c_init(void);

/**
 * @brief 初始化
 *
 */
void Inf_QMI8658A_Init(void);

/**
 * @brief 读取芯片基础信息
 *
 */
Com_Status Inf_QMI8658A_ChipInfo(void);

/**
 * @brief 读取加速度和陀螺仪
 *
 * @param p 倾角结构体 指针
 */
void qmi8658_Read_AccAndGry(t_sQMI8658 *p);

// 获取XYZ轴的倾角值
void qmi8658_fetch_angleFromAcc(t_sQMI8658 *p);

/**
 * @brief 读取数据
 *
 * @param reg_addr 数据地址
 * @param data 数据
 * @param len 数据长度
 * @return esp_err_t
 */
esp_err_t Inf_QMI8658A_register_read(uint8_t reg_addr, uint8_t *data, size_t len);

/**
 * @brief 写入数据
 *
 * @param reg_addr 寄存器地址
 * @param data 数据
 * @return esp_err_t
 */
esp_err_t Inf_QMI8658A_register_write_byte(uint8_t reg_addr, uint8_t data);

#endif /* __INF_QMI8658A_H__ */
